// pagesMine/pages/reportAdd/reportAdd.js
import ajax from "../../../utils/index"
let dataTimeArr = []

for (let index = 0; index < 24; index++) {
  const item = index >= 10 ? index : `0${index}`
  dataTimeArr.push(`${item}:00`)
  dataTimeArr.push(`${item}:30`)
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    /**
     * 字段校验
     */
    isSubmit: false,
    /**
     * 时间范围
     */
    dataTime: dataTimeArr,
    /**
     * 自定义Picker
     */
    showPickerColumn: false,

    /**
     * 表单
     */
    formData: {
      id: null,
      checkReason: null, // 房状态4时的审核原因
      customerCompany: null, // 租客公司名称
      customerMobile: null, // 租客手机号码
      customerName: null, // 租客联系人姓名
      gardenId: null, // 园区id
      gardenName: null, // 园区名称
      keyInfo: null, // 平台返回给经纪人钥匙密码等
      lookHousingStatus: null, // 状态：0报备中；1进行中（报备审核通过）；3已完成；3发放佣金；4报备审核拒绝
      lookTime: null, // 看房时间 记录用 不作为传参
      lookTimeLabel: null, // 格式化时间 记录用 不作为传参
      beginTime: null, // 看房开始时间
      endTime: null, // 看房结束时间
      notes: null, // 备注
      orderNo: null, // 房订单号
      houseArea: null, // 需求面积
      orderStatus: null, // 订单状态：0下订单；1成交； 2订单失败
    }
  },
  /**
   * 校验字段
   */
  isSubmitChange() {
    const { customerCompany, customerMobile, customerName, gardenId, gardenName, beginTime, houseArea } = this.data.formData;
    const boo = !(customerCompany && customerMobile && customerName && gardenId && gardenName && beginTime && houseArea);
    this.setData({ isSubmit: !boo })
  },
  /**
   * input 变化事件
   */
  formItemChange(event) {
    const { formData } = this.data
    const key = event.currentTarget.dataset.key
    const value = event.detail
    formData[key] = value
    this.setData({ formData })
    this.isSubmitChange()
  },
  pickerColumnCancel () {
    this.setData({ showPickerColumn: false })
  },
  /**
   * 时间范围确认事件
   */
  pickerColumnConfirm(event) {
    const { formData } = this.data
    const { valueList, startValue, endValue } = event.detail
    console.log('event.detail', event.detail)
    formData.lookTimeLabel = `${formData.lookTime} ${valueList.join('')}`
    formData.beginTime = `${formData.lookTime} ${startValue}:00`
    formData.endTime = `${formData.lookTime} ${endValue}:00`
    this.setData({ formData, showPickerColumn: false })
    this.isSubmitChange()
  },
  /**
   * 日期选择器事件
   * formData.lookTime 记录日期, 待时间范围选择后 进行拼接 
   * showPickerColumn 显示时间范围
   */
  bindDateChange(event) {
    this.setData({ showPickerColumn: true, 'formData.lookTime': event.detail })
  },
  /**
   * 提交事件
   */
  async submitEvent() {
    const { id, customerCompany, customerMobile, customerName, gardenId, gardenName, beginTime, houseArea } = this.data.formData;
    if (!gardenId || !gardenName) return ajax.showToast('请选择园区');
    if (!customerCompany) return ajax.showToast('请输入公司名称');
    if (!customerName) return ajax.showToast('请输入联系人姓名');
    if (!ajax.regExp.chinese.test(customerName)) return ajax.showToast('请输入正确的联系人姓名');
    if (!customerMobile) return ajax.showToast('请输入联系电话');
    if (!ajax.regExp.phone.test(customerMobile)) return ajax.showToast('请输入正确的联系电话');
    if (!houseArea) return ajax.showToast('请输入需求面积');
    if (!beginTime) return ajax.showToast('请选择看房时间');
    ajax.showLoading();
    const apiUrl = id ? '/workorder/lookHousingOrder/update' : '/workorder/lookHousingOrder/add'
    await ajax.post(apiUrl, this.data.formData).finally(() => wx.hideLoading());
    await ajax.showToast('提交成功, 请等待审核');
    wx.navigateBack()
  },
  /**
   * 获取工单详情
   */
  async getDetailRequest() {
    let { formData } = this.data
    const res = await ajax.get(`/workorder/lookHousingOrder/info?id=${formData.id}`)
    const { lookHousingOrder, garden } = res.data
    const [date, beginTime, endTime] = [ajax.dayjs(lookHousingOrder.beginTime).format("YYYY-MM-DD"), ajax.dayjs(lookHousingOrder.beginTime).format("HH:ss"), ajax.dayjs(lookHousingOrder.endTime).format("HH:ss")];
    (formData = lookHousingOrder, formData.lookHousingStatus = 0, formData.gardenName = garden.gardenName, formData.lookTime = beginTime, formData.lookTimeLabel = `${date} ${beginTime} - ${endTime}`)
    this.setData({ formData })
    this.isSubmitChange()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.formData.id = options.detailId || null
    if (this.data.formData.id) this.getDetailRequest()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const { housing, formData } = this.data
    if (housing) {
      formData.gardenId = housing.gardenId
      formData.gardenName = `${housing.gardenName}${housing.roomNumber}`
      formData.housingAssessId = housing.housingAssess
      formData.houseAgentId = housing.houseAgentId
      this.data.housing = null
      this.setData({ formData })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})